#!/bin/bash
########## 定义变量 ##########
read -p "输入docker服务地址（回车默认43.138.61.146 ） " host
if [ -z "${host}" ];then
        host=43.138.61.146
fi

read -p "输入docker路径（回车默认/var/lib/docker/tmp ） " docker_path
if [ -z "${docker_path}" ];then
        docker_path=/var/lib/docker/tmp
fi

read -p "输入redis密码(回车默认123456)：" redis_passwd
if [ -z "${redis_passwd}" ];then
        redis_passwd=123456
fi

read -p "输入redis端口（默认6380）：" redis_port
if [ -z "${redis_port}" ];then
        redis_port=6380
fi


read -p "输入宿舍系统jar包位置（默认/var/lib/docker/tmp/backend）：" jar_backend_path
if [ -z "${jar_backend_path}" ];then
        jar_backend_path=/var/lib/docker/tmp/backend
fi

read -p "输入宿舍系统jar包对外端口（默认8889）：" jar_backend_port
if [ -z "${jar_backend_port}" ];then
        jar_backend_port=8889
fi

read -p "输入mysql路径（回车默认/var/lib/docker/tmp/mysql）：" mysql_path
if [ -z "${mysql_path}" ];then
        mysql_path=/var/lib/docker/tmp/mysql
fi

read -p "输入mysql对外端口（默认6666）：" mysql_port
if [ -z "${mysql_port}" ];then
        mysql_port=6666
fi

read -p "输入mysql的root用户密码（默认123456）" mysql_root_pass
if [ -z "${mysql_root_pass}" ];then
        mysql_root_pass=mysql
fi


read -p "输入mysql账号（默认root）" mysql_user
if [ -z "${mysql_user}" ];then
        mysql_user=root
fi

read -p "输入mysql账号密码（默认mysql）：" mysql_pass
if [ -z "${mysql_pass}" ];then
        mysql_pass=mysql
fi

read -p "输入mysql连接的数据库（dormitorysystem,若修改则需要在mysql容器中对数据库名称修改）：" mysql_database
if [ -z "${mysql_database}" ];then
        mysql_database=dormitorysystem
fi

read -p "输入rabbitmq账号（默认admin）：" rabbitmq_user
if [ -z "${rabbitmq_user}" ];then
        rabbitmq_user=admin
fi

read -p "输入rabbitmq账号密码（默认123456）：" rabbitmq_pass
if [ -z "${rabbitmq_pass}" ];then
        rabbitmq_pass=123456
fi

read -p "输入rabbitmq对外管理端口（默认15672）：" rabbitmq_port15672
if [ -z "${rabbitmq_port15672}" ];then
        rabbitmq_port15672=15672
fi

read -p "输入rabbitmq端口（默认5672）：" rabbitmq_port5672
if [ -z "${rabbitmq_port5672}" ];then
        rabbitmq_port5672=5672
fi

############## yml文件 ##################


cd ${jar_backend_path}

jar xf dormitorySystem.jar BOOT-INF/classes/application.properties

cat > BOOT-INF/classes/application.properties <<EOF
server.port=8889
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://${host}:${mysql_port}/${mysql_database}?serverTimezone=Asia/Shanghai&useSSL=false&useUnicode=true&characterEncoding=UTF8&useSSL=false
spring.datasource.username=root
spring.datasource.password=${mysql_pass}
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver


spring.redis.database=0

spring.redis.host= ${host}

spring.redis.port=${redis_port}
spring.redis.password= ${redis_passwd}
spring.redis.lettuce.pool.max-active=32
spring.redis.lettuce.pool.max-idle=16
spring.redis.lettuce.pool.min-idle=8


spring.rabbitmq.host=${host}
spring.rabbitmq.port=${rabbitmq_port5672}
spring.rabbitmq.username=${rabbitmq_user}
spring.rabbitmq.password=${rabbitmq_pass}
spring.rabbitmq.virtual-host=/

mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

EOF

jar uf dormitorySystem.jar BOOT-INF/classes/application.properties
rm -rf BOOT-INF/


cat > ${docker_path}/docker-compose.yml << EOF
version: "3.1"
services:
  backend:
    build:
      context: ${jar_backend_path}
      dockerfile: Dockerfile
    image: docker-compose-backend
    container_name: backend
    ports:
      - ${jar_backend_port}:8889
    restart: always
  
  
  redis:
    image: redis:latest
    container_name: docker_redis
    ports:
      - ${redis_port}:6379
    command: ["redis-server","/etc/redis/redis.conf"]
    restart: always
    volumes:
      - ${docker_path}/redis/conf:/etc/redis
      - ${docker_path}/redis/data:/data

  nginx:
    image: nginx:latest
    container_name: docker_nginx
    ports:
      - 9001:80
    volumes:
      - ${docker_path}/nginx/nginx.conf:/etc/nginx/nginx.conf  #指定配置文件
      - ${docker_path}/nginx/log:/var/log/nginx
      - ${docker_path}/nginx/html:/usr/share/nginx/html
    restart: always

  mysql:
    build:
      context: ${mysql_path}
      dockerfile: Dockerfile
    image: docker-compose-mysql
    container_name: docker_mysql
    environment:
      MYSQL_DATABASE: dormitorysystem
      MYSQL_ROOT_PASSWORD: ${mysql_root_pass}
      MYSQL_USER: ${mysql_user}
      MYSQL_PASSWORD: ${mysql_pass}
    ports:
      - ${mysql_port}:3306
    volumes:
      - ./mysql/data:/var/lib/mysql
    restart: always

  rabbitmq:
    image: rabbitmq:3.11.2-management
    container_name: docker_rabbitmq
    environment:
      RABBITMQ_DEFAULT_USER: ${rabbitmq_user}
      RABBITMQ_DEFAULT_PASS: ${rabbitmq_pass}
    ports:
      - ${rabbitmq_port15672}:15672
      - ${rabbitmq_port5672}:5672
EOF

mkdir ${docker_path}/nginx/log -p
mkdir ${docker_path}/nginx/html -p
cat > ${docker_path}/nginx/nginx.conf << EOF
#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  ' -  [] "" '
    #                  '  "" '
    #                  '"" ""';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  43.138.61.146;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;



        location ~/sys/{
            proxy_pass http://43.138.61.146:8889;
        }

        location ~/team/{
            proxy_pass http://43.138.61.146:8889;
        }

        location ~/auth/{
            proxy_pass http://43.138.61.146:8889;
        }

        location ~/order/{
            proxy_pass http://43.138.61.146:8889;
        }

        location ~/user/{
            proxy_pass http://43.138.61.146:8889;
        }

        location ~/room/{
            proxy_pass http://43.138.61.146:8889;
        }






        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}


EOF
chmod 644 ${docker_path}/nginx/nginx.conf
chmod 755 ${docker_path}/nginx/log
chmod 755 ${docker_path}/nginx/html




mkdir ${docker_path}/redis/conf -p
chmod 755  ${docker_path}/redis/conf
cat > ${docker_path}/redis/conf/redis.conf << EOF
bind 0.0.0.0
port ${redis_port}
tcp-backlog 511
timeout 0
tcp-keepalive 0
#loglevel notice
#logfile "/redis/logs/redis.log"
databases 16
requirepass ${redis_passwd}
EOF
chmod 644  ${docker_path}/redis/conf/redis.conf


cd ${docker_path}
docker-compose up -d

docker ps

